﻿#region

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class SettleBooking : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int bookingId;
            if (int.TryParse(Request.QueryString["bookingId"], out bookingId))
            {
                if (IsPostBack)
                {
                    tbDelivery.Text = hdnDelivery.Value;
                    tbReturn.Text = hdnReturn.Value;
                }
                else
                {
                    string bookingType = BookingBLL.GetBookingType(bookingId);
                    switch (bookingType)
                    {
                        case null:
                            Response.Redirect("Default.aspx");
                            break;
                        case "PICK & DROP":
                        {
                            PickAndDropBooking booking = PickAndDropBookingBLL.GetBooking(bookingId);
                            dvPickAndDrop.Visible = true;
                            dvPickAndDrop.DataSource = new List<PickAndDropBooking> {booking};
                            dvPickAndDrop.DataBind();
                        }
                            break;
                        default:
                        {
                            PickBooking booking = PickBookingBLL.GetBooking(bookingId);
                            dvPick.Visible = true;
                            dvPick.DataSource = new List<PickBooking> {booking};
                            dvPick.DataBind();
                            trReturn.Visible = false;
                        }
                            break;
                    }
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void ibReset_OnClick(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
        }

        protected void ibReject_OnClick(object sender, ImageClickEventArgs e)
        {
            BookingBLL.ChangeStatus(Convert.ToInt32(Request.QueryString["bookingId"]), "Rejected");
            Response.Redirect("Default.aspx");
        }

        protected void ibApproved_OnClick(object sender, ImageClickEventArgs e)
        {
            if (IsValid)
            {
                int bookingId = Convert.ToInt32(Request.QueryString["bookingId"]);

                BookingBLL.SettleBooking(bookingId, Convert.ToInt32(tbDistance.Text),
                    Convert.ToDateTime(tbDelivery.Text));

                if (trReturn.Visible)
                    PickAndDropBookingBLL.SetReturnedDate(bookingId, Convert.ToDateTime(tbReturn.Text));

                foreach (
                    Booking_Vehicle_Driver bvd in
                        Booking_Vehicle_DriverBLL.GetVehicleAndDriverDetailsByBookingId(bookingId))
                {
                    DriverBLL.SetAvailability(bvd.DriverId, true);
                    VehicleBLL.SetAvailability(bvd.VehicleRegistrationNo, true);
                }

                Response.Redirect("Default.aspx");
            }
        }
    }
}